﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using MShop.Core.DataAccess;
using MShop.Core.Util;
using MShop.Core.Domain;
using MShop.Core;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class UploadController : SecureController
    {
        private readonly IProductDao _ProductDao;
        private readonly IShopContext _ShopContext;
        public UploadController(IProductDao productDao, IShopContext shopContext)
        {
            _ProductDao = productDao;
            _ShopContext = shopContext;
        }

        public class Thumbnail
        {
            public Thumbnail(string id, byte[] data)
            {
                this.ID = id;
                this.Data = data;
            }


            private string id;
            public string ID
            {
                get
                {
                    return this.id;
                }
                set
                {
                    this.id = value;
                }
            }

            private byte[] thumbnail_data;
            public byte[] Data
            {
                get
                {
                    return this.thumbnail_data;
                }
                set
                {
                    this.thumbnail_data = value;
                }
            }


        }

        public ActionResult Index(int id)
        {
            System.Drawing.Image thumbnail_image = null;
            System.Drawing.Image original_image = null;
            System.Drawing.Bitmap final_image = null;
            System.Drawing.Graphics graphic = null;
            MemoryStream ms = null;

            try
            {
                // Get the data
               
                HttpPostedFileBase jpeg_image_upload = Request.Files["Filedata"];

                // Retrieve the uploaded image
                original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);

                // Calculate the new width and height
                int width = original_image.Width;
                int height = original_image.Height;
                int target_width = _ShopContext.Settings.ImageWidth;
                int target_height = _ShopContext.Settings.ImageHeight;
                int new_width, new_height;

                float target_ratio = (float)target_width / (float)target_height;
                float image_ratio = (float)width / (float)height;

                if (target_ratio > image_ratio)
                {
                    new_height = target_height;
                    new_width = (int)Math.Floor(image_ratio * (float)target_height);
                }
                else
                {
                    new_height = (int)Math.Floor((float)target_width / image_ratio);
                    new_width = target_width;
                }

                new_width = new_width > target_width ? target_width : new_width;
                new_height = new_height > target_height ? target_height : new_height;


                // Create the thumbnail

                // Old way
                //thumbnail_image = original_image.GetThumbnailImage(new_width, new_height, null, System.IntPtr.Zero);
                // We don't have to create a Thumbnail since the DrawImage method will resize, but the GetThumbnailImage looks better
                // I've read about a problem with GetThumbnailImage. If a jpeg has an embedded thumbnail it will use and resize it which
                //  can result in a tiny 40x40 thumbnail being stretch up to our target size


                final_image = new System.Drawing.Bitmap(target_width, target_height);
                graphic = System.Drawing.Graphics.FromImage(final_image);
                graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.Rectangle(0, 0, target_width, target_height));
                int paste_x = (target_width - new_width) / 2;
                int paste_y = (target_height - new_height) / 2;
                graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; /* new way */
                //graphic.DrawImage(thumbnail_image, paste_x, paste_y, new_width, new_height);
                graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height);

                // Store the thumbnail in the session (Note: this is bad, it will take a lot of memory, but this is just a demo)
                ms = new MemoryStream();
                final_image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                Product product = _ProductDao.GetById(id);
                int maxlimit = product.ProductImg.Count;
                if (maxlimit < 10)
                {
                    ///保存到文件夹
                    string imgUrl = IOUtil.Save(jpeg_image_upload);
                    ProductImg img = new ProductImg();
                    img.Created = DateTime.Now;
                    img.Modified = DateTime.Now;
                    img.Url = imgUrl;
                    img.Sort = maxlimit + 1;
                  
                 
                    if (maxlimit == 0)
                    {
                        img.IsDefault = true;
                        product.PicUrl = img.Url;
                    }
                    else
                    {
                        img.IsDefault = false;
                    }
                     product.ProductImg.Add(img);
                    _ProductDao.SaveChanges();
                }
               
                // Store the data in my custom Thumbnail object
                string thumbnail_id = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                Thumbnail thumb = new Thumbnail(thumbnail_id, ms.GetBuffer());

                // Put it all in the Session (initialize the session if necessary)			
                List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;
                if (thumbnails == null)
                {
                    thumbnails = new List<Thumbnail>();
                    Session["file_info"] = thumbnails;
                }
                thumbnails.Add(thumb);

                Response.StatusCode = 200;
                Response.Write(thumbnail_id);
            }
            catch(Exception ex)
            {
               // throw ex;
                // If any kind of error occurs return a 500 Internal Server error
                Response.StatusCode = 500;
                Response.Write("An error occured");
                Response.End();
            }
            finally
            {
                // Clean up
                if (final_image != null) final_image.Dispose();
                if (graphic != null) graphic.Dispose();
                if (original_image != null) original_image.Dispose();
                if (thumbnail_image != null) thumbnail_image.Dispose();
                if (ms != null) ms.Close();
                Response.End();
            }

            return new EmptyResult();
        }


        public ActionResult Thumbnial(string id)
        {
           
            if (id == null)
            {
                Response.StatusCode = 404;
                Response.Write("Not Found");
                Response.End();
                
            }

            List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;

            if (thumbnails == null)
            {
                Response.StatusCode = 404;
                Response.Write("Not Found");
                Response.End();
             
            }

            foreach (Thumbnail thumb in thumbnails)
            {
                if (thumb.ID == id)
                {
                    Response.ContentType = "image/jpeg";
                    Response.BinaryWrite(thumb.Data);
                    Response.End();
                   
                }
            }

            // If we reach here then we didn't find the file id so return 404
            Response.StatusCode = 404;
            Response.Write("Not Found");
            Response.End();
            return new EmptyResult();
        }

    }
}
